package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mapper.CommentMapper;
import cn.tedu.egweibo1.pojo.dto.CommentDto;
import cn.tedu.egweibo1.pojo.entity.Comment;
import cn.tedu.egweibo1.pojo.vo.CommentVo;
import cn.tedu.egweibo1.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 1.发布评论
     */
    @PostMapping("insert")
    public int insert(@RequestBody CommentDto commentDto, HttpSession session){
        System.out.println("commentDto = " + commentDto);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            return 2;
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDto,comment);
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());
        commentMapper.insert(comment);
        return 1;
    }

    /**
     * 2.获取评论功能
     */
    @GetMapping("selectByWeiboId")
    public List<CommentVo> selectByWeiboId(Long weiboId){
        System.out.println("weiboId = " + weiboId);
        return commentMapper.selectByWeiboId(weiboId);
    }
}
